﻿var FoodManager = cc.Class.extend({
    pattern: 1,
    gameScene: null,
    foods: null,
    ctor:function(Scene) {
        this.foods = [];
        this.gameScene = Scene;
    },
    update1:function(hero, elapsed) {
        this.createPattern();
        this.createFood();
        this.foodAnimate(hero);
    },
    createPattern: function (elapsed) {
        this.pattern = Math.ceil(Math.random() * 15);
        if (this.pattern == 7 || this.pattern == 8 || this.pattern == 9)
            this.pattern = 1;
    },
    createFood: function(elapsed) {
        var item = null;
        var winSize = cc.director.getWinSize();
        var yPos = Math.floor(Math.random() * (winSize.height - 200)) + 100;
        switch(this.pattern) {
            case 1:
                
                console.log("type:1");
                if (this.foods.length < 100) {
                    item = Item.create(Math.ceil(Math.random() * 5));
                    item.x = winSize.width - 40;
                    item.y = yPos;
                    this.gameScene.addChild(item);
                    this.foods.push(item);
                }
                break;
            case 2:
                this._patternPosYstart = this._patternPosYstart;
                console.log("type:2");
                
                var i = 0;
                while (i < 8) {
                    item = Item.create(Math.ceil(Math.random() * 5));
                    item.x = winSize.width+item.width;
                    item.y = winSize.height-100-item.height*5*i/3.0;
                    this.gameScene.addChild(item);
                    this.foods.push(item);
                    ++i;
                }
                break;
        }
    },
    foodAnimate: function (hero) {
        for (var i = this.foods.length - 1; i >= 0; --i) {
            var item = this.foods[i];
            item.x -= 10;
            if (item.x < -80) {
                this.foods.splice(i, 1);
                this.gameScene.removeChild(item);
                continue;
            }
            var heroItem_xDist = item.x - hero.x;
            var heroItem_yDist = item.y - hero.y;
            var heroItem_sqDist = heroItem_xDist * heroItem_xDist + heroItem_yDist * heroItem_yDist;
            if (heroItem_sqDist < 5000) {
                this.foods.splice(i, 1);
                this.gameScene.removeChild(item);
                continue;
            }
        }
        
    }
});